package com.bdqn.t320.ch13.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.t320.ch13.entity.Role;
import com.bdqn.t320.ch13.entity.User;
import com.bdqn.t320.ch13.service.RoleService;
import com.bdqn.t320.ch13.service.UserService;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.management.relation.RoleList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    UserService userService;

    @Resource
    RoleService roleService;

    @RequestMapping(value = "/list")
    public Object list(@RequestParam(value = "pageIndex", required = false, defaultValue = "1") Integer pageIndex,
                       @RequestParam(value = "usrName", required = false, defaultValue = "") String usrName,
                       @RequestParam(value = "roleId", required = false, defaultValue = "0") Long roleId,
                       Model model) {
        Page page = new Page<User>(pageIndex, 5);
        User user = new User();
        user.setUsrName(usrName);
        user.setUsrRoleId(roleId);
        userService.getCustomPageUserListBiz(page, user);

        List<Role> roleList = roleService.getRoleListBiz();
        model.addAttribute("roles", roleList);

        model.addAttribute("page", page);
        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("usrName", usrName);
        model.addAttribute("roleId", roleId);
        return "user/list";
    }

    @GetMapping("/toadd")
    public String toAdd(Model model) {
        List<Role> roleList = roleService.getRoleListBiz();
        model.addAttribute("roles", roleList);
        return "/user/add";
    }

    @PostMapping("/save")
    public String save(User user) {
        boolean bool = userService.save(user);
        if (bool == false) {
            return "/user/add";
        } else {
            return "redirect:/user/list";
        }
    }

    @ResponseBody
    @GetMapping("/del")
    public Object del(Integer usrId) {
        return userService.removeById(usrId);
    }

    @GetMapping("/userId")
    public Object userId(Integer usrId, Model model) {
        User user = userService.getUserByIdBiz(usrId);
        model.addAttribute("user", user);
        return "/user/view";
    }

    @PostMapping("/edit")
    public Object edit(User user, Model model) throws Exception {
        int count = userService.updateByIdBiz(user);
        if (count > 0) {
            return "redirect:/user/list";
        } else {
            throw new Exception("出现异常");
        }
    }

    @ExceptionHandler(value = {ArithmeticException.class})
    public String arithmeticException(Model model, ArithmeticException e) {
        model.addAttribute("msg", "局部异常:" + e.getMessage());
        return "/error/error.html";
    }
}